/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.Ghe_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class Ghe_DAO {
    
    public static int Get_MaGhe() throws SQLException{
        int MaGH = 1;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call ADD_ID(?)}");
        statement.setString(1, "GH");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaGH = Rs.getInt(1);
        }
        return MaGH;
    }
    
    public static ArrayList<Ghe_DTO> GetALL()
    {
        ArrayList<Ghe_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from Ghe" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                Ghe_DTO Ghe = new Ghe_DTO();
                Ghe.setMaGhe(rs.getInt("MaGhe"));
                Ghe.setTrangThai(rs.getInt("TrangThai"));
                Ghe.setMaToa(rs.getString("MaToa"));
                ds.add(Ghe);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(Ghe_DTO Ghe)
    {
        boolean flag = false;
        try {
            String sql = "Insert Ghe Values('"+Ghe.getMaGhe()+"','"+Ghe.isTrangThai()+"','"+ Ghe.getMaToa() +"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(Ghe_DTO Ghe)
    {
        boolean flag = false;
        try {
            String sql = "Update Ghe Set TrangThai = '"+ Ghe.isTrangThai() +"', MaToa = '"+Ghe.getMaToa()+"' where MaGhe = "+ Ghe.getMaGhe();
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(int maGhe)
    {
        boolean flag = false;
        try {
            String sql = "Delete from Ghe where MaGhe = "+ maGhe;
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
